package com.tuling.test.tx;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


/**
	 UserServiceProxy--->代理对象--->代理对象.target=普通对象
	 代理对象.test()
	 class UserServiceProxy extends UserService{

	 	UserService target;

	 	public void test(){
			//1.判断当前方法是否标注了@Transaction注解
			//2.创建一个数据库连接conn(事务管理器创建)
			//3.conn.autocommit = false;
			target.test()
 			//4.conn.commit
 			//5.conn.rollback
	 	}
	 }
 */
@Component
public class UserService{

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Autowired
	private UserServiceBase userServiceBase;

	@Transactional
	public void test(){
		jdbcTemplate.update("INSERT INTO t1 VALUES (?, ?, ?, ?, ?)",
				"1","1","1","1","1");
		//throw new NullPointerException();
		/**
		 方式一: 使用AopContext.currentProxy(),需要在配置类上加@EnableAspectJAutoProxy(exposeProxy = true)
		 */
		//((UserService) AopContext.currentProxy()).a();
		/**
		 方式二: 抽取此方法到一个单独的service中
		 */
		userServiceBase.a();
	}

	/*
	@Transactional(propagation = Propagation.NEVER)
	public void a(){

	}
	*/
}
